package com.neuedu.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：      CrossDomain
 * 类名:       MySessionListner
 * 创建时间：  2020/12/17  15:58
 * 描述 :     会话监听
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@WebListener
public class MySessionListner implements HttpSessionListener {

    //当前登录的会话
    private List<HttpSession> sessionList = new ArrayList();



    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext application = se.getSession().getServletContext();
        application.setAttribute("sessionList",sessionList);



        System.out.println("session创建: MySessionListner.sessionCreated");
        sessionList.add(se.getSession());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("session销毁: MySessionListner.sessionDestroyed");
        sessionList.remove(se.getSession());
    }
}
